/*
 * disp_key.c
 *
 *  Created on: 13.10.2012
 *      Author: md5sum@alexsp.ru
 */

#include "global.h"
#include "disp_key.h"
#include "winstar.h"
#include "m41t81.h"
#include "times.h"
#include <util/atomic.h>

#define DISP_LEN 16

static uint8_t str[DISP_LEN+1];
static uint8_t disp_array[DISP_LEN*2][2];
static uint8_t dx=0;
static uint8_t dy=0;
static uint8_t disp_start=0;
static uint8_t prn_start=0;
static uint8_t shift=0;
#define SHIFT_STOP 0
#define SHIFT_RIGHT 1
#define SHIFT_LEFT 2


typedef enum{DISP_FIRST,DISP_MAIN,DISP_DT,DISP_ENVIR,DISP_EDIT_TEMP,
	D_ED_PREPARE,D_ED_YEAR,D_ED_MONTH,D_ED_DAY,D_ED_HOUR,D_ED_MINUTES,D_ED_CALIBR,D_ED_COMMIT,
	D_ED_SETT_PREPARE,D_SETT_SELECT_DW,D_SETT_SELECT_TIME,D_SETT_SELECT_TEMP,D_SETT_SELECT_TIME_NEXT,D_ED_SETT_EXIT,
} display_window;
typedef struct{
	display_window disp;
	uint8_t prev;
	uint8_t next;
	uint8_t exit;
	uint8_t enter;
}menu_t;
PROGMEM menu_t menu_tree[]={
		/*0*/	{DISP_MAIN,.next=1,.prev=0xff,.enter=0xff,.exit=0xff},
		/*1*/	{DISP_DT,.next=2,.prev=0,.enter=10,.exit=0},
		/*2*/	{DISP_ENVIR,.next=11,.prev=1,.enter=0xff,.exit=0},

		/*3*/	{D_ED_DAY,.next=0xfe,.prev=0xfe,.enter=4,.exit=1},
		/*4*/	{D_ED_MONTH,.next=0xfe,.prev=0xfe,.enter=5,.exit=1},
		/*5*/	{D_ED_YEAR,.next=0xfe,.prev=0xfe,.enter=6,.exit=1},
		/*6*/	{D_ED_HOUR,.next=0xfe,.prev=0xfe,.enter=7,.exit=1},
		/*7*/	{D_ED_MINUTES,.next=0xfe,.prev=0xfe,.enter=8,.exit=1},
		/*8*/	{D_ED_CALIBR,.next=0xfe,.prev=0xfe,.enter=9,.exit=1},
		/*9*/	{D_ED_COMMIT,.next=0xff,.prev=0xff,.enter=1,.exit=0xff},
		/*10*/	{D_ED_PREPARE,.next=0xff,.prev=0xff,.enter=3,.exit=0xff},

		/*11*/	{D_ED_SETT_PREPARE,.next=0xff,.prev=2,.enter=12,.exit=0},
		/*12*/	{D_SETT_SELECT_DW,.next=0xfe,.prev=0xfe,.enter=13,.exit=16},
		/*13*/	{D_SETT_SELECT_TIME,.next=0xfe,.prev=0xfe,.enter=14,.exit=12},
		/*14*/	{D_SETT_SELECT_TEMP,.next=0xfe,.prev=0xfe,.enter=15,.exit=12},
		/*15*/	{D_SETT_SELECT_TIME_NEXT,.next=0xff,.prev=0xff,.enter=0xff,.exit=0xff},
		/*16*/	{D_ED_SETT_EXIT,.next=0xff,.prev=0xff,.enter=0xff,.exit=0xff},
		/*17*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0},
		/*18*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0},
		/*19*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0},
		/*20*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0},
		/*21*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0},
		/*22*/	{DISP_EDIT_TEMP,.next=0xff,.prev=2,.enter=0xff,.exit=0xff},

};
#define _D_ED_DAY 3
#define _DISP_DT 1
#define _D_SETT_SELECT_TIME 13
#define _D_SETT_SELECT_TEMP 14
#define _D_SETT_SELECT_TIME_NEXT 15
#define _D_ED_SETT_PREPARE 11

uint8_t menu_ind=0;
display_window current_win=DISP_MAIN;
uint8_t *ed_pointer;
uint8_t ed_min;
uint8_t ed_max;


/*П*/ #define _G_ 0xa8
/*п*/ #define _g_ 0xbe
/*н*/ #define _y_ 0xbd
/*т*/ #define _n_ 0xbf
/*Ч*/ #define _X_ 0xab
/*б*/ #define _be_ 0xb2
/*м*/ #define _v_ 0xbc
/*я*/ #define _z_ 0xc7
/*Д*/ #define _L_ 0xe0
/*д*/ #define _l_ 0xe3
/*з*/ #define _p_ 0xb7
/*в*/ #define _d_ 0xb3
/*Э*/ #define _EE_ 0xaf
/*и*/ #define _b_ 0xb8
/*к*/ #define _r_ 0xba
/*щ*/ #define _o_ 0xe6
/*У*/ #define _E_ 0xa9
/*л*/ #define _k_ 0xbb
/*ь*/ #define _m_ 0xc4
/*г*/ #define _u_ 0xb4
/*ц*/ #define _w_ 0xe5
/*ш*/ #define _i_ 0xc1

prog_char vremya_str[]={'B','p','e',_v_,_z_,0};
prog_char data_str[]={_L_,'a',_n_,'a',0};
prog_char temper_str[]={'T','e',_v_,_g_,'.',' ',0};
prog_char vozduh_str[]={_d_,'o',_p_,_l_,'y','x',':',0};
prog_char ten_str[]={'T',_EE_,'H',':',0};
prog_char radiat_str[]={'p','a',_l_,'.',':',0};
prog_char tekush_str[]={'T','e',_r_,'y',_o_,':',0};
prog_char korrek[]="Kopp: ";
prog_char ustanovka[]={_E_,'c',_n_,'a',_y_,'o',_d_,_r_,'a',' ','t',0x08,0};
prog_char po_vremeni[]={_g_,'o',' ',_d_,'p','e',_v_,'e',_y_,_b_,0};
prog_char error_ds[]={'O',_i_,_b_,_be_,_r_,'a','!',0};
prog_char nagrevatel[]={'T','E','H',' ',0};
prog_char troomerror[]={'T',0x08,_r_,'o',_v_,' ',0};
prog_char theaterror[]={'T',0x08,_n_,'a',_u_,' ',0};
prog_char tradiatorerr[]={'T',0x08,'p','a',_l_,0};

/*дни недели              0   1   2   3   4   5   6   7   8   9   0   */
prog_char ponedel[]=	{_G_,'o',_y_,'e',_l_,'e',_k_,_m_,_y_,_b_,_r_,0};
prog_char vtornik[]=	{'B',_n_,'o','p',_y_,_b_,_r_,0};
prog_char sreda[]=		{'C','p','e',_l_,'a',0};
prog_char chetverg[]=	{_X_,'e',_n_,_d_,'e','p',_u_,0};
prog_char pyatnica[]=	{_G_,_z_,_n_,_y_,_b_,_w_,'a',0};
prog_char subbota[]=	{'C','y',_be_,_be_,'o',_n_,'a',0};
prog_char voskres[]=	{'B','o','c',_r_,'p','e','c','e',_y_,_m_,'e',0};

#define ponedel2	{_G_,_y_,0}
#define vtornik2 	{'B',_n_,0}
#define sreda2 		{'C','p',0}
#define chetverg2	{_X_,_n_,0}
#define pyatnica2	{_G_,_n_,0}
#define subbota2	{'C',_be_,0}
#define voskres2	{'B','c',0}

#define ponedel3	{_G_,_y_,_l_,0}
#define vtornik3	{'B',_n_,'p',0}
#define sreda3		{'C','p',_l_,0}
#define chetverg3	{_X_,_n_,_d_,0}
#define pyatnica3	{_G_,_n_,_y_,0}
#define subbota3	{'C',_be_,_n_,0}
#define voskres3	{'B','c',_r_,0}

prog_char *day_of_week[7]={voskres,ponedel,vtornik,sreda,chetverg,pyatnica,subbota};
prog_char day_of_week2[7][3]={voskres2,ponedel2,vtornik2,sreda2,chetverg2,pyatnica2,subbota2};
prog_char day_of_week3[7][4]={voskres3,ponedel3,vtornik3,sreda3,chetverg3,pyatnica3,subbota3};

uint8_t * time2str(const tm *, const uint8_t *, const uint8_t sec);
uint8_t * date2str(const tm *, const uint8_t *, const uint8_t year);
uint8_t * dayw2str(const uint8_t, const uint8_t, const uint8_t *);
uint8_t * temp2str(int16_t t, uint8_t * str);
uint8_t * w10_2str(const uint8_t t,const uint8_t wd, const uint8_t *s);
uint16_t abs_ (int16_t t);
static void button_press(uint8_t button);

static void prepare_left(){
	uint8_t i;
	prn_start=0;
	if (disp_start!=0) return;
	for (i=0;i<DISP_LEN;i++) {
		disp_array[i+DISP_LEN][0]=disp_array[i][0];
		disp_array[i+DISP_LEN][1]=disp_array[i][1];
	}
	disp_start=DISP_LEN;
}
static void prepare_right(){
	uint8_t i;
	prn_start=DISP_LEN;
	if (disp_start!=DISP_LEN) return;
	for (i=0;i<DISP_LEN;i++) {
		disp_array[i][0]=disp_array[i+DISP_LEN][0];
		disp_array[i][1]=disp_array[i+DISP_LEN][1];
	}
	disp_start=0;
}

static void check_button(){
#define CNT 50
#define LONG_CNT 0x2FF
	static uint8_t cnt=0;
	static uint16_t long_cnt=0;
	static uint8_t button=0;
	uint8_t tmp;
	tmp=~(PINC|0x0f);
	if (!tmp) {
		cnt=0;
		return;
	}
	if (tmp==button) {
		if (cnt<CNT) {
			cnt++;
		}
		if (cnt==CNT-1){
			button_press(button);
			long_cnt=0;
		}
		if ((button==K_RIGHT)||(button==K_LEFT)){
			if (cnt==CNT) {
				long_cnt++;
				if (long_cnt==LONG_CNT) {
					long_cnt=LONG_CNT-LONG_CNT/4;
					button_press(button);
				}
			}
		}
	}else{
		cnt=0;
		//		long_cnt=0;
		button=tmp;
	}
#undef CNT
}

void disp_step() {
	static uint8_t tmp_x=0;

	if (!tmp_x) {
		if (shift==SHIFT_LEFT) {
			if (disp_start>0) {disp_start--;}else{shift=SHIFT_STOP;}
			//			if (disp_start>0) {disp_start--;}else{shift=SHIFT_STOP;}
		}
		if (shift==SHIFT_RIGHT) {
			if (disp_start<DISP_LEN) {disp_start++;}else{shift=SHIFT_STOP;}
			//			if (disp_start<DISP_LEN) {disp_start++;}else{shift=SHIFT_STOP;}
		}
	}
	LCD_xy(tmp_x,0);
	LCD_char(disp_array[tmp_x+disp_start][0]);
	LCD_xy(tmp_x,1);
	LCD_char(disp_array[tmp_x+disp_start][1]);
	tmp_x++;
	if (tmp_x==DISP_LEN) tmp_x=0;
	if (shift==SHIFT_STOP) check_button();
}

void display_str_p(prog_char *str){
	prog_char *p;
	char a;
	p=(prog_char *)str;

	while ((a=pgm_read_byte(p++))&&(dx<DISP_LEN)) {
		disp_array[prn_start+(dx++)][dy]=a;
	}

}

void display_str(uint8_t *str){
	uint8_t *p;
	p=(uint8_t *)str;
	while ((*p!=0)&&(dx<DISP_LEN)) {
		disp_array[prn_start+(dx++)][dy]=*(p++);
	}
}

void display_xy(uint8_t col, uint8_t row){
	if (col<DISP_LEN) dx=col;
	if (row<2) dy=row;
}

void display_init(){
	LCD_init();
}

void display_clear(){
	uint8_t i;
	for (i=0;i<DISP_LEN;i++) {
		disp_array[i+prn_start][0]=' ';
		disp_array[i+prn_start][1]=' ';
	}
	display_xy(0,0);
}

void display_flush(){
	uint8_t tmp;
	for (tmp=0;tmp<DISP_LEN;tmp++) disp_step();
}

/*Преобразования в текст*/
uint8_t * w10_2str(const uint8_t t,const uint8_t wd,const uint8_t *s){
	uint8_t * tmp;
	uint8_t h,m;
	if (t<144) {
	h=t/6;
	m=t%6;
	tmp=(uint8_t *)s;
	*(tmp++)=h/10 +'0';
	*(tmp++)=h%10 +'0';
	*(tmp++)=':';
	*(tmp++)=m +'0';
	*(tmp++)='0';
	*(tmp++)=0;
	}else{
		dayw2str(wd<6?wd+1:0,3,s);
		tmp=(uint8_t *)&s[3];
		*(tmp++)='.';
		*(tmp++)=' ';
		*(tmp++)=0;
	}
	return (uint8_t *)s;
}

uint8_t * time2str(const tm *t,const uint8_t *s, const uint8_t sec){
	uint8_t * tmp;
	tmp=(uint8_t *)s;
	*(tmp++)=t->hour/10 +'0';
	*(tmp++)=t->hour%10 +'0';
	if (sec) {
		*(tmp++)=':';
	}else{
		if (t->sec & 0x01) *(tmp++)=':'; else *(tmp++)=' ';
	}
	*(tmp++)=t->min/10 +'0';
	*(tmp++)=t->min%10 +'0';
	if (sec) {
		*(tmp++)=':';
		*(tmp++)=t->sec/10 +'0';
		*(tmp++)=t->sec%10 +'0';
	}
	*(tmp++)=0;
	return	(uint8_t *)s;
}

uint8_t * date2str(const tm *t,const uint8_t *s,const uint8_t year){
	uint8_t * tmp;
	tmp=(uint8_t *)s;
	*(tmp++)=t->day/10 +'0';
	*(tmp++)=t->day%10 +'0';
	*(tmp++)='.';
	*(tmp++)=t->month/10 +'0';
	*(tmp++)=t->month%10 +'0';
	if (year) *(tmp++)='.';
	if (year>1) {
		*(tmp++)='2';
		*(tmp++)='0';
	}
	if (year) {
		*(tmp++)=t->year/10 +'0';
		*(tmp++)=t->year%10 +'0';
	}
	*(tmp++)=0;
	return	(uint8_t *)s;
}
uint8_t * dayw2str(const uint8_t wd,const uint8_t num,const uint8_t *s){
	uint8_t * tmp;
	prog_char * tmpi;
	tmp=(uint8_t *)s;

	if (num<3) tmpi=&day_of_week2[wd][0]; else tmpi=&day_of_week3[wd][0];
	do {
	*tmp=pgm_read_byte(tmpi++);
	}while (*(tmp++)!=0);
	return (uint8_t *)s;
}

uint16_t abs_ (int16_t t){
	if (t<0) return (uint16_t)(t*(-1)); else
		return (uint16_t)t;
}

uint8_t * temp2str(int16_t t, uint8_t * str){
	uint8_t *p;
	p=str;
	if (abs_(t)>99) {
		*(p++)='E';
		*(p++)='r';
		*(p++)='r';
		*(p++)=0;
		return str;}
	if (t>=0) *(p++)='+'; else *(p++)='-';
	t=abs_(t);
	*(p++)=t/10 +'0';
	*(p++)=t%10 +'0';
	*p=0;
	return str;
}

uint8_t * uint8_to_str(const uint8_t a, uint8_t * str, uint8_t size, char space){
	uint8_t tmp_a=a;
	uint8_t tmp;
	uint8_t *p;
	p=str;
	if (size>3) return str;
	if (size<1) return str;
	if (size==3) {
		tmp=tmp_a/100;
		*(p++)=(tmp>0?tmp+'0':space);
		tmp_a=tmp_a % 100;
	}
	if (size>=2) {
		tmp=tmp_a/10;
		*(p++)=(tmp>0?tmp+'0':space);
		tmp_a=tmp_a %10;
	}
	*(p++)=tmp_a+'0';
	*p=0;
	return str;
}

static void button_press(uint8_t button){
	uint8_t tmp;
	switch (button) {
	case K_RIGHT:
		tmp=pgm_read_byte(&menu_tree[menu_ind].next);
		if (tmp==0xff) break;
		if (tmp==0xfe) {
			if ((*ed_pointer)<ed_max) {(*ed_pointer)++;}else{*ed_pointer=ed_min;}
			display();
			break;
		}
		prepare_right();
		menu_ind=tmp;
		current_win=pgm_read_byte(&menu_tree[menu_ind].disp);
		display();
		shift=SHIFT_RIGHT;
		break;
	case K_LEFT:
		tmp=pgm_read_byte(&menu_tree[menu_ind].prev);
		if (tmp==0xff) break;
		if (tmp==0xfe) {
			if ((*ed_pointer)>ed_min) {(*ed_pointer)--;}else{*ed_pointer=ed_max;}
			display();
			break;
		}
		prepare_left();
		menu_ind=tmp;
		current_win=pgm_read_byte(&menu_tree[menu_ind].disp);
		display();
		shift=SHIFT_LEFT;
		break;
	case K_EXIT:
		tmp=pgm_read_byte(&menu_tree[menu_ind].exit);
		if (tmp==0xff) break;
		menu_ind=tmp;
		current_win=pgm_read_byte(&menu_tree[menu_ind].disp);
		display();
		break;
	case K_ENTER:
		tmp=pgm_read_byte(&menu_tree[menu_ind].enter);
		if (tmp==0xff) break;
		menu_ind=tmp;
		current_win=pgm_read_byte(&menu_tree[menu_ind].disp);
		display();
		break;
	default:
		break;
	}
}
uint8_t * m41control2str(uint8_t * a,uint8_t * s){
	uint8_t * tmp;
	uint8_t b;
	tmp=s;
	if (*a<0x20) {
		*(tmp++)='-';
		b=0x20-(*a);
	}else{
		*(tmp++)='+';
		b=(*a)-0x20;
	}
	*(tmp++)=b/10 +'0';
	*(tmp++)=b%10 +'0';
	*(tmp)=0;
	return s;
}

void display_edit_dt(){
	display_xy(1,0);
	display_str(date2str(&tmp_time,str,1));
	display_str((uint8_t *)" ");
	display_str(time2str(&tmp_time,str,0));
	display_xy(1,1);
	display_str_p(korrek);
	display_str(m41control2str(&m41_control,str));

}
void display_edit_time_temp(uint8_t tmp_index,uint8_t time_index,uint8_t time_temp) {
	display_clear();
	display_str(dayw2str(tmp_index,3,str));
	display_str((uint8_t *)" ");
	if (!time_temp) display_str((uint8_t *)"\x02"); else display_str((uint8_t *)" ");
/*
	display_str(uint8_to_str(times_array[tmp_index][time_index].time/6,str,2,'0'));
	display_str((uint8_t *)":");
	display_str(uint8_to_str((times_array[tmp_index][time_index].time%6)*10,str,2,'0'));
*/
	display_str(w10_2str(times_array[tmp_index][time_index].time,tmp_index,str));
	if (!time_temp) display_str((uint8_t *)"\x03"); else display_str((uint8_t *)" ");
	if (time_temp) display_str((uint8_t *)"\x02"); else display_str((uint8_t *)" ");
	display_str(temp2str(times_array[tmp_index][time_index].termo,str));
	if (time_temp) display_str((uint8_t *)"\x03");
	display_xy(0,1);
	display_str(uint8_to_str(time_index+1,str,2,' '));
	display_str((uint8_t *)"/");
	display_str(uint8_to_str(get_count_t_on_day(tmp_index),str,2,' '));

	display_str((uint8_t *)" ");
	display_str((uint8_t *)"\x01");
	if (time_index<POINTS_ON_DAY-1) {
/*
		display_str(uint8_to_str(times_array[tmp_index][time_index+1].time/6,str,2,'0'));
		display_str((uint8_t *)":");
		display_str(uint8_to_str((times_array[tmp_index][time_index+1].time%6)*10,str,2,'0'));
*/
		display_str(w10_2str(times_array[tmp_index][time_index+1].time,tmp_index,str));
	}else{
		display_str(w10_2str(144,tmp_index,str));
	}

}

void display() {
	static display_window last=DISP_FIRST;
	static uint8_t tmp_index,time_index;

	if (last!=current_win) display_clear();
	last=current_win;
	if (main_status & (STATUS_DS_ERR|STATUS_HEAT_ERR)) {
		display_clear();
		display_xy(0,0);
		display_str_p(error_ds);
		display_xy(0,1);
		if (main_status & STATUS_HEAT_ERR) {
			display_str_p(nagrevatel);
			return;
		}
		if (room_err_cnt) {
			display_str_p(troomerror);
		}
		if (heater_err_cnt) {
			display_str_p(theaterror);
		}
		if (radiator_err_cnt) {
			display_str_p(tradiatorerr);
		}
		return;
	}
	switch (current_win) {
	case DISP_MAIN:
		display_xy(0,0);
		display_str(dayw2str(current_time.day_w,3,str));
		display_str((uint8_t *)" ");
		display_str(date2str(&current_time,str,0));
		display_str((uint8_t *)"  ");
		display_str(time2str(&current_time,str,0));
		display_xy(11,1);
		display_str(temp2str(room_temp,str));
		display_str((uint8_t *)"\x08""C");
		display_xy(0,1);
		if (current_time.sec&0x04) {
			display_str(w10_2str(next_time,current_time.day_w,str));
			display_str((uint8_t *)"\x01");
			display_str(temp2str(next_temp,str));
		}else{
			display_str_p(tekush_str);
			display_str(temp2str(current_temp,str));
		}
		break;
	case D_ED_COMMIT:
		m41_control=(m41_control<=0x20)?(0x20-m41_control):((m41_control-0x20)|0x20);
		m41stop();
		current_time=tmp_time;
		m41set_time(&current_time);
		//		m41calibrate(&m41_control);
		m41start();
		time_read(&current_time);
		menu_ind=_DISP_DT;
		current_win=DISP_DT;
		//no break
	case DISP_DT:
		display_xy(0,0);
		display_str_p(data_str);
		display_str((uint8_t *)"  ");
		display_str(date2str(&current_time,str,2));
		display_xy(0,1);
		display_str_p(vremya_str);
		display_str((uint8_t *)"   ");
		display_str(time2str(&current_time,str,1));
		break;
	case DISP_ENVIR:
		display_xy(0,0);
		display_str_p(temper_str);
		display_str_p(vozduh_str);
		display_str(temp2str(room_temp,str));
		display_xy(0,1);
		display_str_p(ten_str);
		display_str(temp2str(heater_temp,str));
		display_str((uint8_t *)" ");
		display_str_p(radiat_str);
		display_str(temp2str(radiator_temp,str));
		break;

		/*Установка часов*/
	case D_ED_PREPARE:
		tmp_time=current_time;
		tmp_time.sec=0;
		while (m41_get_calibrate(&m41_control)!=SUCCESS) {}
		m41_control&=0x3f;
		if (m41_control&0x20) {
			m41_control=0x20+(m41_control&0x1f);
		}else{
			m41_control=0x20-m41_control;
		}
		menu_ind=_D_ED_DAY;
		current_win=D_ED_DAY;
		//no break
	case D_ED_DAY:
		display_edit_dt();
		ed_pointer=&tmp_time.day;
		ed_min=1;
		ed_max=31;
		display_xy(0,0);
		display_str((uint8_t *)"\x02");
		display_xy(3,0);
		display_str((uint8_t *)"\x03");
		break;
	case D_ED_MONTH:
		display_edit_dt();
		ed_pointer=&tmp_time.month;
		ed_min=1;
		ed_max=12;
		display_xy(3,0);
		display_str((uint8_t *)"\x02");
		display_xy(6,0);
		display_str((uint8_t *)"\x03");
		break;
	case D_ED_YEAR:
		display_edit_dt();
		ed_pointer=&tmp_time.year;
		ed_min=12;
		ed_max=99;
		display_xy(6,0);
		display_str((uint8_t *)"\x02");
		display_xy(9,0);
		display_str((uint8_t *)"\x03");
		break;
	case D_ED_HOUR:
		display_edit_dt();
		ed_pointer=&tmp_time.hour;
		ed_min=0;
		ed_max=23;
		display_xy(9,0);
		display_str((uint8_t *)"\x02");
		display_xy(12,0);
		display_str((uint8_t *)"\x03");
		break;
	case D_ED_MINUTES:
		display_edit_dt();
		ed_pointer=&tmp_time.min;
		ed_min=0;
		ed_max=59;
		display_xy(12,0);
		display_str((uint8_t *)"\x02");
		display_xy(15,0);
		display_str((uint8_t *)"\x03");
		break;
	case D_ED_CALIBR:
		display_edit_dt();
		ed_pointer=&m41_control;
		ed_min=1;
		ed_max=0x3f;
		display_xy(6,1);
		display_str((uint8_t *)"\x02");
		display_xy(10,1);
		display_str((uint8_t *)"\x03");
		break;

		/*настройки температур*/
	case D_ED_SETT_EXIT:
		store_config();
		current_temp=get_termo_for_time(current_time.ten_m,current_time.day_w);
		menu_ind=_D_ED_SETT_PREPARE;
		current_win=D_ED_SETT_PREPARE;
		//no break
	case D_ED_SETT_PREPARE:
		display_xy(2,0);
		display_str_p(ustanovka); //  Установка ot
		display_xy(3,1);
		display_str_p(po_vremeni);//   по времени
		tmp_index=1;
		break;
	case D_SETT_SELECT_DW:
		ed_pointer=&tmp_index;
		ed_max=6;
		ed_min=0;
		display_clear();
		display_str_p(day_of_week[tmp_index]);
		display_xy(12,0);
		display_str((uint8_t *)"(");
		display_str(uint8_to_str(get_count_t_on_day(tmp_index),str,2,' '));
		display_str((uint8_t *)")");
		time_index=0;
		break;
	case D_SETT_SELECT_TIME_NEXT:
		L_NEXT_TIME:
		if (times_array[tmp_index][time_index].time==0x90) {
			times_clear_to_end(tmp_index,time_index);
			time_index--;
		}else{
			time_index++;
			if (time_index==POINTS_ON_DAY) time_index=0;
		}

	if (times_array[tmp_index][time_index].time>=0x90) {
		times_array[tmp_index][time_index].termo=times_array[tmp_index][time_index-1].termo;
	}

	menu_ind=_D_SETT_SELECT_TIME;
	current_win=D_SETT_SELECT_TIME;
	//no break
	case D_SETT_SELECT_TIME:
		ed_pointer=&times_array[tmp_index][time_index].time;
		ed_min=times_array[tmp_index][time_index-1].time+1;
		ed_max=(time_index<POINTS_ON_DAY-1)?times_array[tmp_index][time_index+1].time-1:0x90;
		if (ed_max==143) ed_max++;
//		if (*ed_pointer==ed_max) times_array[tmp_index][time_index].termo=times_array[tmp_index][time_index-1].termo;
		if (time_index>0) {
			display_edit_time_temp(tmp_index,time_index,0);

			break;
		}else{
			current_win=D_SETT_SELECT_TEMP;
			menu_ind=_D_SETT_SELECT_TEMP;
		}
		//no break
	case D_SETT_SELECT_TEMP:

		if (times_array[tmp_index][time_index].time==0x90) {
			goto L_NEXT_TIME;
		}

		ed_pointer=(uint8_t *)&(times_array[tmp_index][time_index].termo);
		ed_min=0;
		ed_max=40;
		display_edit_time_temp(tmp_index,time_index,1);
		break;
	default:
		break;
	}
}


